<!-- 装修图文组件：广告魔方 -->
<template>
  <view class="home">
			<view class="top">
				<view class="ss-cube-wrap">
				  <view
				    class="cube-img-wrap"
				    :style="[parseImgStyle(data.list[0]), { margin: '20rpx' }]"
				    @tap="sheep.$router.go(data.list[0].url)"
				  >
				    <image class="cube-img" :src="sheep.$url.cdn(data.list[0].imgUrl)" mode="aspectFill"></image>
				  </view>
						<view
						  class="cube-img-wrap"
								 style="width: 50%;"
						  :style="[parseImgStyle(data.list[1]), { margin: '20rpx' }]"
						  @tap="sheep.$router.go(data.list[1].url)"
						>
						  <image class="cube-img" :src="sheep.$url.cdn(data.list[1].imgUrl)" mode="aspectFill"></image>
						</view>
				</view>
				<view class="ss-cube-wrap">
				  <view
				    class="cube-img-wrap"
				    :style="[parseImgStyle(data.list[2]), { margin: '20rpx' }]"
				    @tap="sheep.$router.go(data.list[2].url)"
				  >
				    <image class="cube-img" :src="sheep.$url.cdn(data.list[2].imgUrl)" mode="aspectFill"></image>
				  </view>
						<view
						  class="cube-img-wrap"
								 style="width: 50%;"
						  :style="[parseImgStyle(data.list[3]), { margin: '20rpx' }]"
						  @tap="sheep.$router.go(data.list[3].url)"
						>
						  <image class="cube-img" :src="sheep.$url.cdn(data.list[3].imgUrl)" mode="aspectFill"></image>
						</view>
				</view>
			</view>
    <!-- <view v-for="(item, index) in data.list" :key="index">
      <view
        class="cube-img-wrap"
        :style="[parseImgStyle(item), { margin: data.space + 'px' }]"
        @tap="sheep.$router.go(item.url)"
      >
        <image class="cube-img" :src="sheep.$url.cdn(item.imgUrl)" mode="aspectFill"></image>
      </view>
    </view> -->
  </view>
</template>
<script setup>
  /**
/**
 * 广告魔方
 *
 * @property {Array<Object>} list 			- 魔方列表
 * @property {Object} styles 				- 组件样式
 * @property {String} background 			- 组件背景色
 * @property {Number} topSpace 				- 组件顶部间距
 * @property {Number} bottomSpace 			- 组件底部间距
 * @property {Number} leftSpace 			- 容器左间距
 * @property {Number} rightSpace 			- 容器右间距
 * @property {Number} imgSpace 				- 图片间距
 * @property {Number} imgTopRadius 			- 图片上圆角
 * @property {Number} imgBottomRadius 		- 图片下圆角
 *
 */

  import { computed, inject, unref } from 'vue';
  import sheep from '@/sheep';

  // 参数
  const props = defineProps({
    data: {
      type: Object,
      default() {},
    },
    styles: {
      type: Object,
      default() {},
    },
  });

  // 单元格大小
  const windowWidth = sheep.$platform.device.windowWidth;
  const cell = computed(() => {
    return (
      (windowWidth -
        ((props.styles.marginLeft || 0) + (props.styles.marginRight || 0) + (props.styles.padding || 0) * 2)) /
      4
    );
  });

  //包裹容器高度
  const parseAdWrap = computed(() => {
    let heightArr = props.data.list.reduce(
      (prev, cur) => (prev.includes(cur.height + cur.top) ? prev : [...prev, cur.height + cur.top]),
      [],
    );
    let heightMax = Math.max(...heightArr);
    return {
      height: heightMax * cell.value + 'px',
      width:
        windowWidth -
        (props.data?.style?.marginLeft +
          props.data?.style?.marginRight +
          props.styles.padding * 2) *
          2 +
        'px',
    };
  });

  // 解析图片大小位置
  const parseImgStyle = (item) => {
    let obj = {
      width: item.width * cell.value - props.data.space + 'px',
      height: item.height * cell.value - props.data.space + 'px',
      left: item.left * cell.value + 'px',
      top: item.top * cell.value + 'px',
      'border-top-left-radius': props.data.borderRadiusTop + 'px',
      'border-top-right-radius': props.data.borderRadiusTop + 'px',
      'border-bottom-left-radius': props.data.borderRadiusBottom + 'px',
      'border-bottom-right-radius': props.data.borderRadiusBottom + 'px',
    };
    return obj;
  };
</script>

<style lang="scss" scoped>
	.home{
		position: relative;
		z-index: 2;
	}
	.top{
		margin-bottom: 20rpx;
		border-bottom: 1px solid #c7c7c7;
		.ss-cube-wrap {
				display: flex;
				justify-content: space-between;
		  width: 49.2%;
				height: 90rpx;
				border-right: 1px solid #c7c7c7;
				// border-bottom: 1px solid #e3e3e3;
		}

		.cube-img-wrap {
		  position: absolute;
		  z-index: 3;
		  overflow: hidden;
				margin-top: 40rpx;
		}

		.cube-img {
		  width: 100%;
		  height: 100%;
		}
	}
</style>
